﻿#define _CRT_SECURE_NO_WARNINGS
#include"Date.h"
bool Date::CheckDate()
{
	if (_month >= 1 && _month <= 12 &&
		_day>=1&&_day<=GetMonthDay(_year,_month))
	{
		
		return true;
	}
	else
	{
		return false;
	}
}
void Date::print()
{
	cout << _year << '/' << _month << '/' << _day << endl;
}
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!CheckDate())
	{
		cout << "日期非法：";
		print();
		assert(CheckDate());
	}
}
bool Date::operator<(const Date& d)const
{
	if (_year < d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month < d._month)
		{
			return true;
		}
		else if (_month==d._month)
		{
			if (_day < d._day)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}

}
bool Date ::operator==(const Date& d)const
{
	return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator<=(const Date& d)const
{
	return *this < d || *this == d;
}
bool  Date::operator>(const Date& d)const
{
	return !(*this == d) && !(*this < d);
}
bool Date::operator>=(const Date& d)const
{
	return !(*this < d);
}
bool Date::operator!=(const Date & d) const
{
	return !(*this == d);
}
 /*d1 += 天数
+=会改变第一个操作数*/
//这里我们改变了当前对象中成员变量的值因此我们不要对this指针加cosnt
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		day = -day;
		return *this -= day;
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
//因为加不改变第一个操作数所以加上const
//这里返回值不能是引用因为我们要返回加完天数之后的
//对象会被销毁如果返回引用会造成空引用
Date Date::operator+(int day) const
{
	if (day < 0)
	{
		day = -day;
		return *this - day;
	}
	//调用拷贝构造
	Date tem = *this;
	Date tem2(*this);
	tem += day;
	return tem;
}
// d1 -= 天数
//
Date&Date:: operator-=(int day)
{
	if (day < 0)
	{
		day = -day;
		return *this += day;
	}
	_day -= day;
	while (_day < 1)
	{
		_month--;
		if (_month == 0)
		{
			_month = 12;
			_year--;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

//d1-100
Date Date::operator-(int day) const
{
	if (day < 0)
	{
		day = -day;
		return *this + day;
	}
	Date tmp(*this);
	tmp -= day;
	return tmp;
}
Date& Date::operator++()
{
	return (*this +=1);
}
// d1++ -> d1.operator++(0)
// 为了区分，构成重载，给后置++，强⾏增加了⼀个int形参
// 这⾥不需要写形参名，因为接收值是多少不重要，也不需要⽤
// 这个参数仅仅是为了跟前置++构成重载区分
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}

Date& Date::operator--()
{
	return (*this -= 1);
}
Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}
// 重载
//返回值是为了保证连续性
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << '/' << d._month << '/' << d._day << endl;
	return out;
}
istream& operator>>(istream& in, Date& d)
{
	
		cout << "请依次输入年月日:>" << endl;;
		in >> d._year >> d._month >> d._day;
		while (!d.CheckDate())
		{
			cout << "日期非法:"<<endl;
			d.print();
			cout << "请重新输入" << endl;
			in >> d._year >> d._month >> d._day;
		}
		return in;
}
int Date::operator-(const Date& d) const
{
	Date max(*this);
	Date min(d);
	int falg = 1;
	if (max < min)
	{
		falg = -1;
		//构造函数
		Date tem = max;
		//赋值重载
		max = min;
		//赋值重载
		min = tem;
	}
	int count = 0;
	while (min < max)
	{
		++min;
		count++;
	}

	return count*falg;
}